~ OOP,Classes,Python,Understanding polymorphism in Python


A look at polymorphism in Python

*Please Note* - all presentations and code challenges+solutions are available to subscribing members. It's now time to look at the pillar of object orientated programming referred to as polymorphism. You should also be becoming quite familiar with how classes work in practice, and how you can use them in your projects. Do watch the video below which explains the concept, goes through the coding example and then attempt the challenges which are presented inside the code yourself

Definition: It is the characteristic of an entity to be able to present in more than one form (have multiple behaviours)

Real world example: Think of identical twins. They look the same, but they don't necessarily act the same do they? In Python: Consider the '+' operator. It can be used to sum two integers, but also to concatenate. The addition operator looks the same, but behaves differently. This makes it polymorphic.

Polymorphic property of overriding: Overriding: (in the real world it would be a bit like when you inherit a characteristic or abiilty from your parent (like say dancing) BUT the way you implement this ability is slightly different. In the same way in Python certain methods can be changed in the child class to behave differently to how it did in the parent class.

Note: Having finished this introduction to OOP, you may want to start with creating games with tkinter or pygame - see our solve and learn lessons and teaching resources that will guide you through this

Video

Code and Challenge #1

"""
POLYMORPHISM

-Definition: It is the characteristic of an entity to be able to present in more than one form (have multiple behaviours)

Real world example: Think of identical twins. They look the same, but they don't necessarily act the same do they?
In Python: Consider the '+' operator. It can be used to sum two integers, but also to concatenate. The
addition operator looks the same, but behaves differently. This makes it polymorphic. 

Polymorphic property of overriding: Overriding: (in the real world it would be a bit like when you inherit
a characteristic or abiilty from your parent (like say dancing) BUT the way you implement this ability
is slightly different. In the same way in Python certain methods can be changed in the child class to
behave differently to how it did in the parent class. 

TASK:
Create a class called Square and perform the following tasks -
(i) Create two objects for this class squareOne and squareTwo
(ii) Find the result of side of squareOne to the power of side of squareTwo
Example: If squareOne has length of 2cm each side and
squareTwo has a length of 4cm each side, squareOne **
squareTwo should return 16, which is 2 to the power of 4.
Hint: While performing SquareOne ** SquareTwo, you need to
overload __pow__() method
"""
class Student:
    #Two methods, one that sets the number of courses taken, and one to display the no. of courses
    def setNoOfCourses(self):
        self.numberofcourses = 3

    def displayNoOfCourses(self):
        print(self.numberofcourses)

#====An advanced student is a genius and may want to take more courses than the average student
class AdvancedStudent(Student):
    #Here we override- change the nature of the base class Method *but using the same name of the method

#====DEMONSTRATING OVERRIDING (redefining the base class method in a child class)
    def setNoOfCourses(self):
        self.numberofcourses=5 #redefining the base class method to behave differently
    #this means that when you call the SetNoOfCourses, it will not only display what the base class has declared, but this has been overridden

    def resetNoOfCourses(self):
        super().setNoOfCourses() #once you call the super function the control is being transfered to the base/parent class.

student=Student()
student.setNoOfCourses()
print("Number of courses taken by the Student : ", end="")
student.displayNoOfCourses()
 #====DEMONSTRATING POLYMORPHISM
advanced_student=AdvancedStudent()
advanced_student.setNoOfCourses()
print("Number of courses taken by the Advanced Student : ", end="")
advanced_student.displayNoOfCourses()
print("Number of courses taken by the Advanced Student after reset (this uses the super function to call the method of the base class again): ", end="")
advanced_student.resetNoOfCourses()
advanced_student.displayNoOfCourses()

Download Python 3 here: